﻿using AmazonMODEL;
using MODEL;
using SysCommon.Log;
using SysController;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace OrdersService
{
    public partial class AmazonOrdersService : ServiceBase, SysCommon.WinServices.IServiceController
    {
        public AmazonOrdersService()
        {
            InitializeComponent();
        }  
        public void Start()
        {
            SysCommon.Log.Log.Write("AmazonOrdersService 服务启动");
        }

        #region 2015-11-08 下载订单 -  void GetOrderList_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        /// <summary>
        /// 2015-11-08 下载订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetOrderList_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var appkey in SystemController.RunAccount)
            {
                List<OrderModel> list = OrdersController.ListOrders(appkey);
                if (list == null || list.Count < 1)
                {
                    Log.Write(string.Format("{0} 没有订单", appkey.AccountInfoName));
                    continue;
                }
                Log.Write(string.Format("{0} 有{1}订单", appkey.AccountInfoName, list.Count));
                bool IsAgain = true;
            Again: int row = SystemController.AddAmazonOrders(list.Select(s => s.Order).ToList());
                if (row < 1)
                {
                    if (IsAgain)
                    {
                        Log.Write(string.Format("添加订单信息异常:【{0}】", row));
                        IsAgain = false;
                        goto Again;
                    }
                    Log.Write(string.Format("添加订单信息异常重试:【{0}】", row));
                    continue;
                }
                else
                {
                    Log.Write("添加订单信息成功！");
                    bool IsAgainAddress = true;
                AgainAddress: int rowAddress = SystemController.AddAmazonOrdersShippingAddress(list.Select(s => s.ShippingAddress).ToList());
                    if (rowAddress < 1)
                    {
                        Log.Write(string.Format("添加订单邮寄地址信息异常:【{0}】", row));
                        if (IsAgainAddress)
                        {
                            IsAgainAddress = false;
                            goto AgainAddress;
                        }
                        Log.Write(string.Format("添加订单邮寄地址信息异常重试:【{0}】", row));
                        continue;
                    }
                    else
                    {
                        Log.Write("添加订单邮寄地址信息成功！");
                    }
                    bool IsAgainTotal = true;
                AgainTotal: int rowTotal = SystemController.AddAmazonOrdersTotal(list.Select(s => s.Total).ToList());
                    if (rowTotal < 1)
                    {
                        if (IsAgainTotal)
                        {
                            IsAgainTotal = false;
                            goto AgainTotal;
                        }
                        Log.Write(string.Format("添加订单金额信息异常重试:【{0}】", row));
                        continue;
                    }
                    else
                    {
                        Log.Write("添加订单邮金额信息成功！");
                    }
                }
            }
        }
        #endregion

        #region 2015-11-08 下载订单明细 - void GetOrderDetailsList_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        /// <summary>
        /// 2015-11-08 下载订单明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetOrderDetailsList_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var appkey in SystemController.RunAccount)
            {
                List<AmazonOrder> list = SystemController.GetAmazonOrders(appkey);
                if (list.Count < 1 || list == null)
                {
                    Log.Write(string.Format("{0} 没有要下载订单明细的订单", appkey.AccountInfoName));
                    continue;
                }
                Log.Write(string.Format("{0} 有{1}订单明细", appkey.AccountInfoName, list.Count));
                List<AmazonOrder> listUpdate = new List<AmazonOrder>();
                foreach (var item in list)
                {
                    List<AmazonOrdersDetail> detailList = OrdersController.ListOrderItems(appkey, item.AmazonOrderId);
                    if (detailList == null || detailList.Count < 1)
                    {
                        Log.Write(string.Format("{0} 没有订单明细", appkey.AccountInfoName));
                        continue;
                    }
                    bool IsAgain = true;
                Again: int row = SystemController.AddAmazonOrdersDetails(detailList);
                    if (row < 1)
                    {
                        //TODO:
                        if (IsAgain)
                        {
                            Log.Write(string.Format("添加订单明细信息异常:【{0}】", row));
                            IsAgain = false;
                            goto Again;
                        }
                        Log.Write(string.Format("添加订单明细信息异常重试:【{0}】", row));
                        continue;
                    }
                    else
                    {
                        Log.Write("添加订单明细信息成功！");
                        item.IsDetails = true;
                        item.UpdateTime = DateTime.Now;
                        item.UpdateUser = "GetOrderDetails";

                        listUpdate.Add(item);
                    }
                }
                bool IsAgainDetails = true;
            AgainDetails: int count = SystemController.ModifyAmazonOrders(listUpdate);
                if (count < 1)
                {
                    if (IsAgainDetails)
                    {
                        IsAgainDetails = false;
                        goto AgainDetails;
                    }
                }
            }
        }
        #endregion

        #region 2015-11-14 上传追踪号 - void CreateOrderFulfillment_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        /// <summary>
        /// 2015-11-14 上传追踪号
        /// </summary> 
        private void CreateOrderFulfillment_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var appkey in SystemController.RunAccount)
            {
                string str = SysCommon.XmlHelper.ObjectToXmlSerializer(OrdersController.OrderFulfillment(appkey));

                FeedsInfo model = new FeedsInfo()
                {
                    AccountInfoId = appkey.AccountInfoID,
                    SiteId = appkey.MarketplacesID ?? -1,
                    AmazonEnvelope = str,
                    CreateTime = DateTime.Now,
                    IsDel = false,
                    AdReleaseIdList = "Order",
                    CreateUser = "CreateFulfillment",
                    UpdateTime = DateTime.Now,
                    UpdateUser = "Fulfillment",
                    FeedProcessingStatus = FeedProcessingStatus.Upload.ToString()
                };
                bool IsAgain = true;
            Again: int row = SystemController.AddFeedsInfo(model);
                if (row < 1)
                {
                    if (IsAgain)
                    {
                        IsAgain = false;
                        goto Again;
                    }
                    continue;
                }
                Log.Write("订单上传追踪号信息创建成功！");
            }
        }
        #endregion
    }
}
